﻿using System;

namespace flygame
{
    internal class Program
    {

        const int boardSize = 30; // 棋盘格数
        static int[] positions = { 0, 0 }; // 两名玩家的位置
        static string[] playerNames = { "玩家1", "玩家2" };
        static int currentPlayer = 0; // 当前玩家索引

        // 定义一个委托
        public delegate void PlayerAction(int index,int position);

        public static void add(int index, int position)
        {
            Console.WriteLine("玩家" + playerNames[index] + "加分:" + position);

            positions[index] += position; // 更新玩家位置
            if (positions[index] > boardSize)
            {
                Console.WriteLine("玩家" + playerNames[index] + "超过棋盘长度，位置归零");
                positions[index] = 0; // 如果超过棋盘长度，位置归零
            }
            else if (positions[index] == boardSize)
            {
                Console.WriteLine("玩家" + playerNames[index] + "到达终点，获胜！");
            }
            else
            {
                Console.WriteLine("玩家" + playerNames[index] + "前进到第 " + positions[index] + " 格。");
            }
        }

        public static Dictionary<int, PlayerAction> playerActions = new Dictionary<int, PlayerAction>
        {
            { 10, add }, // 玩家1的加分方法
            { 12, sub },  // 玩家2的减分方法
            { 20, sub },  // 玩家2的减分方法
        };


        public static void sub(int index, int position)
        {
            Console.WriteLine("玩家" + playerNames[index] + "减分:" + position);
            positions[index] -= position; // 更新玩家位置
            if (positions[index] < 0)
            {
                Console.WriteLine("玩家" + playerNames[index] + "位置小于0，位置归零");
                positions[index] = 0; // 如果位置小于0，位置归零
            }
            else
            {
                Console.WriteLine("玩家" + playerNames[index] + "倒退到第 " + positions[index] + " 格。");
            }
        }

        static void Main(string[] args)
        {
            
            Random rand = new Random();

            Console.WriteLine("=== 飞行棋小游戏 ===");
            Console.WriteLine($"棋盘长度：{boardSize} 格，先到终点者获胜！\n");

            while (true)
            {
                Console.WriteLine($"{playerNames[currentPlayer]} 当前在第 {positions[currentPlayer]} 格。");
                Console.Write($"{playerNames[currentPlayer]} 按回车掷骰子...");
                Console.ReadLine();

                int dice = rand.Next(1, 7); // 掷骰子，生成1到6的随机数
                Console.WriteLine($"{playerNames[currentPlayer]} 掷出了 {dice} 点！");

                positions[currentPlayer] += dice; // 更新玩家位置
                if (positions[currentPlayer] >= boardSize) // 检查是否到达终点
                {
                    Console.WriteLine($"{playerNames[currentPlayer]} 到达终点，获胜！");
                    break;
                }
                else
                {
                    PlayerAction action = null;
                    playerActions.TryGetValue(positions[currentPlayer], out action);
                    action?.Invoke(currentPlayer, positions[currentPlayer]); // 执行对应的玩家动作
                }

                Console.WriteLine($"{playerNames[currentPlayer]} 前进到第 {positions[currentPlayer]} 格。\n");
                currentPlayer = 1 - currentPlayer; // 切换玩家
            }

            Console.WriteLine("游戏结束，感谢游玩！");
        }
    }
}
